package com.zeus.core.sender;

import com.dingtalk.api.request.OapiRobotSendRequest;
import com.zeus.common.enums.ZeusSenderTypeEnum;
import com.zeus.common.log.ZeusLoggerConfig;
import com.zeus.common.utils.MarkdownUtils;
import com.zeus.core.sender.dingtalk.DingTalkRoBot;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 宙斯钉钉发送助手
 *
 * @author f1ybear_wang  2022/2/22 11:53 上午
 */
public class ZeusDingTalkSender extends AbstractZeusSender {
    private final static Logger log = LoggerFactory.getLogger(ZeusDingTalkSender.class);

    /**
     * 钉钉机器人
     */
    private Account account;

    /**
     * 消息内容
     */
    private MarkdownMessage message;

    /**
     * 构造
     * 
     * @param account 钉钉凭证
     * @param message 消息内容
     * @author f1ybear_wang  2022/2/22 12:20 下午
     */
    public ZeusDingTalkSender(Account account, MarkdownMessage message) {
        this.account = account;
        this.message = message;
    }

    @Override
    ZeusSenderTypeEnum senderType() {
        return ZeusSenderTypeEnum.DING_TALK;
    }

    /**
     * 发送消息
     *
     * @author f1ybear_wang  2022/2/22 12:20 下午
     */
    @Override
    boolean sendMessage() {
        if (account == null || message == null) {
            if (log.isWarnEnabled() && ZeusLoggerConfig.needEnableLogger()) {
                log.warn("### 宙斯监控报警，发送钉钉消息失败，凭证或消息内容为空 >> account = {}, message = {}", account, message);
            }
            return true;
        }
        return new DingTalkRoBot(account.getAccessToken(), account.getSecret()).sendMessage(message);
    }

    /**
     * 凭证
     */
    @Data
    public static class Account {
        /**
         * 凭证
         */
        private String accessToken;
        /**
         * 秘钥
         */
        private String secret;
    }

    /**
     * markdown消息
     */
    @Data
    public static class MarkdownMessage {
        /**
         * 消息类型: markdown
         */
        private final String msgType = "markdown";

        /**
         * 显示标题
         */
        private String title;

        /**
         * 显示内容
         */
        private String text;

        /**
         * 是否at所有人
         */
        private Boolean isAtAll = false;

        /**
         * 被@人的手机号(在content里添加@人的手机号)
         */
        private Set<String> atMobiles;

        /**
         * 转为请求参数
         *
         * @return OapiRobotSendRequest
         * @author wangfeixiong126@gmail.com  2021/6/21 22:46
         */
        public OapiRobotSendRequest toRequest() {
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            // 消息类型
            request.setMsgtype(msgType);
            request.setTimestamp(System.currentTimeMillis());

            // at
            OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
            if (isAtAll != null) {
                at.setIsAtAll(isAtAll);
            }
            if (atMobiles != null && !atMobiles.isEmpty()) {
                at.setAtMobiles(new ArrayList<>(atMobiles));

                // 如果@全体了，则忽略@个人
                if (this.text != null && !Boolean.TRUE.equals(this.isAtAll)) {
                    // 消息内容中塞入@成员的信息
                    String atMobileStr = atMobiles.stream().map(item -> "@" + item).collect(Collectors.joining());
                    this.text = this.text.concat(MarkdownUtils.LINE_FEED + atMobileStr);
                }
            }
            request.setAt(at);

            // 内容
            OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
            markdown.setText(text);
            markdown.setTitle(title);
            request.setMarkdown(markdown);
            return request;
        }
    }

}
